<?php

class Usuarios_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_mapper = new Usuarios_Model_UsuarioMapper();
    	$this->view->messages = $this->_helper->flashMessenger->getMessages();
    }

    public function indexAction()
    {
        // action body
    	$this->view->headTitle()->append('Lista de Usuários');
    	$usuarios = $this->_mapper;
    	$listuser = Zend_Paginator::factory ( $usuarios->listUsuarios() );
    	$listuser->setCurrentPageNumber ( $this->_getParam( 'pagina', 1 ) )
    	->setItemCountPerPage ( 10 );
    	$this->view->entries = $listuser;    
    }

    public function editarAction()
    {
        // action body
    	$this->view->headTitle()->append('Editar Usuarios');
    	$form = new Usuarios_Form_Usuarios();
    	$request = $this->getRequest();
    	$id = $request->getParam("id");
    	$page = $request->getParam("pagina");
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->_request->getPost();
    		if ($form->isValid($request->getPost())) {
    			$usuario = new Usuarios_Model_Usuario($form->getValues());
        		$this->_mapper->editUsuario($usuario, $id);
        		$this->_helper->flashMessenger->addMessage('Usuário Editado com sucesso!');
        		$this->_helper->redirector("index","index","usuarios",array("pagina"=>$page));
    		}else{
    			$form->populate($formData);
    		}
    	}else{
    		$data = $this->_mapper->findpopule($id, new Usuarios_Model_Usuario());
    		$form->populate($data);
    	}
    	$this->view->form = $form;
    }

    public function removerAction()
    {
        // action body
    	$this->view->headTitle()->append('Usuário Removido');
    	$request = $this->getRequest();
    	$id = $request->getParam("id");
    	$page = $request->getParam("pagina");
    	$this->_mapper->deletar($id);
    	$this->_helper->flashMessenger->addMessage('Usuário removido com sucesso!');
    	$this->_helper->redirector("index","index","usuarios",array("pagina"=>$page));
    }


}





